# encoding:utf-8
class ListNode:
    def __init__(self, x: int):
        self.val = x    # 节点值
        self.next: Optional[ListNode] = None  # 后继节点引用（指针）


n0 = ListNode(1)
n1 = ListNode(3)
n2 = ListNode(2)
n3 = ListNode(5)
n4 = ListNode(4)

# 构建引用指向
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4

def insert(n0: ListNode, P: ListNode) -> None:
    """
    在链表中插入节点P
    """
    n1 = n0.next
    P.next = n1
    n0.next = P


def delete(n0: ListNode) -> None:
    """
    删除链表中节点 n0 的后继节点
    """
    if not n0.next:
        return
    # n0 -> P -> n1
    P = n0.next
    n1 = P.next
    n0.next = n1


def access(head: ListNode, index: int) -> Optional[ListNode]:
    """
    访问链表中索引为 index 的节点
    """
    for _ in range(index):
        if not head:
            return None
        head = head.next
    return head

def find(head: ListNode, target: int) -> int:
    """
    查找链表中值为 target 的首个节点
    """
    index = 0
    while head:
        if head.val == target:
            return index
        head = head.next
        index += 1
    return -1
